/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.inf.ufg.view.cadastros;

import br.inf.ufg.model.Operadores;
import br.inf.ufg.model.OperadoresDAO;
import br.inf.ufg.model.PontoVendas;
import br.inf.ufg.model.PontoVendasDAO;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author umarley.faria
 */
public class TelaCadastroOperadores extends javax.swing.JFrame {

    /**
     * Creates new form TelaCadastroLocal
     */
    public TelaCadastroOperadores() throws SQLException {
        initComponents();
        iniciarBD();
        primeiro();
        carregaPontos(); 
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        jFrame2 = new javax.swing.JFrame();
        jToolBar1 = new javax.swing.JToolBar();
        btFirst = new javax.swing.JButton();
        btPrev = new javax.swing.JButton();
        btNext = new javax.swing.JButton();
        btLast = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btNovo = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btEditar = new javax.swing.JButton();
        btDeletar = new javax.swing.JButton();
        btPesquisar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        ckAtivo = new javax.swing.JCheckBox();
        ckGerente = new javax.swing.JCheckBox();
        tfCodigo = new javax.swing.JTextField();
        tfMatricula = new javax.swing.JTextField();
        tfNome = new javax.swing.JTextField();
        cbPonto = new javax.swing.JComboBox();
        tfLogin = new javax.swing.JTextField();
        tfSenha = new javax.swing.JPasswordField();
        jLabel7 = new javax.swing.JLabel();
        tfConfSenha = new javax.swing.JPasswordField();

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jFrame2Layout = new javax.swing.GroupLayout(jFrame2.getContentPane());
        jFrame2.getContentPane().setLayout(jFrame2Layout);
        jFrame2Layout.setHorizontalGroup(
            jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame2Layout.setVerticalGroup(
            jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Locais");
        setAlwaysOnTop(true);

        jToolBar1.setRollover(true);

        btFirst.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/resultset_first.png"))); // NOI18N
        btFirst.setToolTipText("Primeiro Registro");
        btFirst.setFocusable(false);
        btFirst.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btFirst.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btFirst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btFirstActionPerformed(evt);
            }
        });
        jToolBar1.add(btFirst);

        btPrev.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/resultset_previous.png"))); // NOI18N
        btPrev.setToolTipText("Anterior");
        btPrev.setFocusable(false);
        btPrev.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btPrev.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btPrev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPrevActionPerformed(evt);
            }
        });
        jToolBar1.add(btPrev);

        btNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/resultset_next.png"))); // NOI18N
        btNext.setToolTipText("Próximo");
        btNext.setFocusable(false);
        btNext.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btNext.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNextActionPerformed(evt);
            }
        });
        jToolBar1.add(btNext);

        btLast.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/resultset_last.png"))); // NOI18N
        btLast.setToolTipText("Último Registro");
        btLast.setFocusable(false);
        btLast.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btLast.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btLast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLastActionPerformed(evt);
            }
        });
        jToolBar1.add(btLast);
        jToolBar1.add(jSeparator1);

        btNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/add.png"))); // NOI18N
        btNovo.setToolTipText("Novo");
        btNovo.setFocusable(false);
        btNovo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btNovo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });
        jToolBar1.add(btNovo);

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/disk.png"))); // NOI18N
        btSalvar.setToolTipText("Salvar");
        btSalvar.setFocusable(false);
        btSalvar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btSalvar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });
        jToolBar1.add(btSalvar);

        btEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/pencil.png"))); // NOI18N
        btEditar.setToolTipText("Editar");
        btEditar.setFocusable(false);
        btEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarActionPerformed(evt);
            }
        });
        jToolBar1.add(btEditar);

        btDeletar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/delete.png"))); // NOI18N
        btDeletar.setToolTipText("Remover");
        btDeletar.setFocusable(false);
        btDeletar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btDeletar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btDeletar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDeletarActionPerformed(evt);
            }
        });
        jToolBar1.add(btDeletar);

        btPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/ufg/view/images/magnifier.png"))); // NOI18N
        btPesquisar.setToolTipText("Procurar");
        btPesquisar.setFocusable(false);
        btPesquisar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btPesquisar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarActionPerformed(evt);
            }
        });
        jToolBar1.add(btPesquisar);

        jLabel1.setText("Código:");

        jLabel2.setText("Matrícula:");

        jLabel3.setText("Ponto:");

        jLabel4.setText("Nome:");

        jLabel5.setText("Login:");

        jLabel6.setText("Senha:");

        ckAtivo.setText("Ativo");
        ckAtivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ckAtivoActionPerformed(evt);
            }
        });

        ckGerente.setText("Gerente");

        tfCodigo.setEditable(false);

        cbPonto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbPontoActionPerformed(evt);
            }
        });

        jLabel7.setText("Confirme a Senha:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 353, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(tfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(tfLogin)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel6))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(tfMatricula)
                                            .addComponent(cbPonto, 0, 151, Short.MAX_VALUE))
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel4)))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(tfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(tfConfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(107, 107, 107)
                        .addComponent(ckAtivo)
                        .addGap(61, 61, 61)
                        .addComponent(ckGerente)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(tfMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cbPonto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7)
                        .addComponent(tfConfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(jLabel6)
                        .addComponent(tfLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(tfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ckAtivo)
                    .addComponent(ckGerente))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
       try
       {
           novo();
       }
       catch( Exception e)
       {
           
       }
    }//GEN-LAST:event_btNovoActionPerformed

    private void ckAtivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ckAtivoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ckAtivoActionPerformed

    private void btFirstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btFirstActionPerformed
        primeiro();
    }//GEN-LAST:event_btFirstActionPerformed

    private void btPrevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPrevActionPerformed
        anterior();
    }//GEN-LAST:event_btPrevActionPerformed

    private void btNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNextActionPerformed
        proximo();
    }//GEN-LAST:event_btNextActionPerformed

    private void btLastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLastActionPerformed
        ultimo();
    }//GEN-LAST:event_btLastActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        salvar();
    }//GEN-LAST:event_btSalvarActionPerformed

    private void btPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarActionPerformed
        pesquisar();
    }//GEN-LAST:event_btPesquisarActionPerformed

    private void btEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarActionPerformed
        habilitaCampos();
    }//GEN-LAST:event_btEditarActionPerformed

    private void btDeletarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDeletarActionPerformed
        try 
        {
            remover();
        }
        catch (SQLException ex) 
        {
            Logger.getLogger(TelaCadastroOperadores.class.getName()).log(Level.SEVERE, null, ex);
        }// TODO add your handling code here:
    }//GEN-LAST:event_btDeletarActionPerformed

    private void cbPontoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbPontoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbPontoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroOperadores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroOperadores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroOperadores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroOperadores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new TelaCadastroOperadores().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(TelaCadastroOperadores.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btDeletar;
    private javax.swing.JButton btEditar;
    private javax.swing.JButton btFirst;
    private javax.swing.JButton btLast;
    private javax.swing.JButton btNext;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btPesquisar;
    private javax.swing.JButton btPrev;
    private javax.swing.JButton btSalvar;
    private javax.swing.JComboBox cbPonto;
    private javax.swing.JCheckBox ckAtivo;
    private javax.swing.JCheckBox ckGerente;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JFrame jFrame2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTextField tfCodigo;
    private javax.swing.JPasswordField tfConfSenha;
    private javax.swing.JTextField tfLogin;
    private javax.swing.JTextField tfMatricula;
    private javax.swing.JTextField tfNome;
    private javax.swing.JPasswordField tfSenha;
    // End of variables declaration//GEN-END:variables
    
    
    private OperadoresDAO operadoresDAO;
    private PontoVendasDAO pontoDAO;
    
    
    private void iniciarBD() {
        
        
        try {
            operadoresDAO = new OperadoresDAO();
            pontoDAO = new PontoVendasDAO();
        } catch ( Exception e ) {
            JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
        }
 }
    
    
     private void remover() throws SQLException
    {
        int resposta = JOptionPane.showConfirmDialog(rootPane, "Deseja Realmente Excluir este registro?", "Pergunta", JOptionPane.CANCEL_OPTION);

        if(resposta == JOptionPane.OK_OPTION)
        {
            
            Operadores operadores = new Operadores();
            operadores.setIdOperador( Integer.parseInt( tfCodigo.getText() ) );

            try
            {
                operadoresDAO.excluirRegistro( operadores );
                primeiro();
            }
            catch(SQLException e)
            {
                JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
            }
        }
    }
     
     
    private void pesquisar()
    {
    
        String criterio = JOptionPane.showInputDialog( this, "Digite o código ou o LOGIN para pesquisar");

        if( !criterio.isEmpty() )
        {
            if( criterio.matches("^\\d+$") )
            {
                
                int codigo = Integer.parseInt( criterio );

                try
                {
                    Operadores operadores = operadoresDAO.getOperadores( codigo );
                
                    if ( operadores != null ) 
                    {
                        mostrarDados( operadores );                    
                        JOptionPane.showMessageDialog( this, "Registro Encontrado." );
                    }
                    else 
                    {
                        JOptionPane.showMessageDialog( this, "Registro Não Encontrado ");
                    }
                }
                catch(Exception e)
                {
                      JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
                } 


            }
            else
            {
                 try
                 {
                    // # DAO 
                    //PontoVendasDAO pontoDAO = new PontoVendasDAO(); 
                    
                    Operadores operadores = operadoresDAO.pesquisarOperadores(criterio);  
                    
                    if ( operadores != null ) 
                    {
                        mostrarDados( operadores );                    
                        JOptionPane.showMessageDialog( this, "Registro Encontrado." );
                    }
                    else 
                    {
                        JOptionPane.showMessageDialog( this, "Registro Não Encontrado ");
                    }
                    
                }
                catch(Exception e)
                {
                     JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
                }
            }
        }
    } 
    
   public void mostrarDados( Operadores operadores ) 
    {
        try
        {
            int idOperador        = operadores.getIdOperador();
            String matricula      = operadores.getMatricula(); 
            String nome           = operadores.getNome(); 
            String login          = operadores.getLogin(); 
            String senha          = operadores.getSenha(); 
            int status            = operadores.getIsAtivo();
            int gerente           = operadores.getIsGerente();

            if( !"".equals(operadores.getPonto()) )
            {
                PontoVendas pontoVenda = pontoDAO.getPontoVenda( Integer.parseInt( operadores.getPonto()) ); 
                cbPonto.setSelectedItem( pontoVenda.getIdPonto()+" - "+pontoVenda.getNomeFantasia() );
            }
            else
            {
                cbPonto.setSelectedItem( "" );
            }
                
            if( status == 1 )
            {
                ckAtivo.setSelected( true );
            }
            else
            {
                ckAtivo.setSelected( false );
            }
            

            if( gerente == 1 )
            {
                ckGerente.setSelected( true );
            }
            else
            {
                ckGerente.setSelected( false );
            }
            
            
            
            tfCodigo.setText( String.valueOf( idOperador ) );
            tfMatricula.setText(String.valueOf ( matricula ) );
            
            tfNome.setText( nome );
            tfLogin.setText( login );
            tfSenha.setText( senha );
               
        }
        catch (Exception e) 
        {
            JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
        }

     }
    
       private void desabilitaCampos()
    {
         // # setar todos os itens como editaveis 
        tfCodigo.setEditable(false);
        tfMatricula.setEditable(false); 
        cbPonto.setEditable(false);
        tfNome.setEditable(false); 
        tfLogin.setEditable(false);
        tfSenha.setEditable(false);
        
    }
   
       private void habilitaCampos()
    {
         // # setar todos os itens como editaveis 
        tfCodigo.setEditable(false);
        tfMatricula.setEditable(true);
        cbPonto.setEditable(true);
        tfNome.setEditable(true);
        tfLogin.setEditable(true); 
        tfSenha.setEditable(true);  
    }
       
       
    public void primeiro() {
    
        desabilitaCampos();
        
        try 
        {
            Operadores operadores = operadoresDAO.primeiro();

            if ( operadores != null) 
            {
                mostrarDados( operadores );
            }                    

        } 
        catch ( SQLException e ) 
        {
            JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
        }
}
    
    public void anterior() 
    {
        
        desabilitaCampos(); 
                
        try 
        {
            Operadores operadores = operadoresDAO.anterior();

            if ( operadores != null) 
            {
                mostrarDados( operadores );
            }                    

        } 
        catch ( SQLException e ) 
        {
            JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
        }

    }
    
    
    public void proximo() 
    {
        desabilitaCampos(); 
        
        try 
        {
           Operadores operadores = operadoresDAO.proximo();

           if ( operadores != null) 
           {
               mostrarDados( operadores );
           }                    

       } 
       catch ( SQLException e ) 
       {
           JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
       }

    }
    
    public void ultimo() 
    {
        desabilitaCampos();
        
        try 
        {
            Operadores operadores = operadoresDAO.ultimo();

            if ( operadores != null) 
            {
                mostrarDados( operadores );
            }                    

        } 
        catch ( SQLException e ) 
        {
          JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
        }
    }

    
    public void novo() throws SQLException
    {
        // # setar todos os itens como editaveis 
        habilitaCampos(); 
        
        // # setar os valores como vazio 
        tfCodigo.setText( null );
        tfMatricula.setText( "" );
        cbPonto.setSelectedItem( "" );
        tfNome.setText( "" );
        tfLogin.setText( "" );
        tfSenha.setText( "" );
    }
    
    // # carrega os pontos 
    public void carregaPontos() throws SQLException
    {
        // # lista de pontos de venda
        ArrayList<PontoVendas> arrPontoVendas = pontoDAO.ListarPontoVendas();
        
        cbPonto.addItem("");
        for ( PontoVendas ponto : arrPontoVendas ) 
        {
            cbPonto.addItem( ponto.getIdPonto()+" - "+ponto.getNomeFantasia() );
        }
    }
    
     // # funcao que salva o registro 
    public void salvar()
    {
        Operadores operadores  = new Operadores(); 

        String senha = tfSenha.getText() ; 
        String confSenha = tfConfSenha.getText();
        
        if( !senha.equals(confSenha) &&  !tfConfSenha.getText().isEmpty() )
        {
            JOptionPane.showMessageDialog( this, "As senhas nao conferem!!");
        }
        else
        {
            // # setar os valores para o objeto      
            operadores.setMatricula( tfMatricula.getText() );
            operadores.setNome( tfNome.getText() );
            operadores.setLogin( tfLogin.getText() );
            operadores.setSenha( tfSenha.getText() );
            
            int isAtivo   = (ckAtivo.isSelected() ? 1 : 0);
            int isGerente = (ckGerente.isSelected() ? 1 : 0);
            
            operadores.setIsAtivo( isAtivo ); 
            operadores.setIsGerente( isGerente );
            
            // # tratando a String do combo para retornar apenas o numero 
            String cbPontoVenda = cbPonto.getSelectedItem().toString(); 
            String parts[] = cbPontoVenda.split(Pattern.quote("-"));
            operadores.setPonto(  parts[0] );
            
            // # verificar se e uma inclusao ou edicao de um registro
            if( tfCodigo.getText().isEmpty() )
            {            
                try
                {   
                    operadoresDAO.salvarRegistro( operadores );
                    JOptionPane.showMessageDialog( this, "Salvo com Sucesso" );
                    primeiro();
                }   
                catch(Exception e)
                {
                    JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
                }
            }
            else
            {
                // # id do operador 
                operadores.setIdOperador( Integer.parseInt(tfCodigo.getText()) );

                try
                {
                    operadoresDAO.atualizarRegistro( operadores );
                    JOptionPane.showMessageDialog( this, "Atualizado com Sucesso" );
                }
                catch(Exception e)
                {
                    JOptionPane.showMessageDialog( this, "Erro: " + e.getMessage() );
                }
            }
        }
    }

}
